<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="服务员 / 查询" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="danger" icon="el-icon-lx-add" @click="func_handleAdd" plain>添加</el-button>
            </div>
            <el-table :data="barberTableData" border class="table" header-cell-class-name="table-header" @selection-change="func_handleSelectionChange">
                <el-table-column type="selection" width="55" />
				<el-table-column prop="id" label="ID" width="55" align="center" v-if="false"></el-table-column>
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="name" label="姓名" width="150" >
                    <template #default="scope">
                        <el-button type="text" @click="func_handleDrawerOpen(scope.$index, scope.row)"> <el-icon class="el-icon-s-unfold" /> {{scope.row.name}} </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="spellName" label="名称" align="center" width="150"/>
                <el-table-column label="工作照" align="center" width="170" >
                    <template #default="scope">
                        <el-avatar class="avatar" shape="square" :src="scope.row.headPortrait"/>
                    </template>
                </el-table-column>
                
                <el-table-column prop="grade" label="等级" align="center"  />
                <el-table-column label="手艺值" align="center" >
                    <template #default="scope">
                        {{ scope.row.craftValue === null ? 0: scope.row.craftValue }}
                    </template>
                </el-table-column>
                <el-table-column prop="workYear" label="工作年限" align="center" ></el-table-column>
                <el-table-column label="操作" :width="optColumnWidth" align="center" >
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-delete" class="red mr10" @click="func_handleDelete(scope.$index, scope.row)">删除</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item @click="func_handleEdit(scope.$index, scope.row)"><el-icon class="el-icon-edit" />编辑</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="func_handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- 详细抽屉 -------------------------------------------------------------------------------------------------------------- -->
        <el-drawer title="详细信息" v-model="drawerViewVisible " direction="rtl" size="68%">
            <div class="handle-box">
                <el-main style="text-align: center">
                    <el-avatar :size="300" :src="barberDrawerForm.headPortrait"  />
                    <el-descriptions title="服务员信息" direction="vertical" :column="2"  border >
                        <el-descriptions-item label="员工编号">{{barberDrawerForm.id}}</el-descriptions-item>
                        <el-descriptions-item label="名称">{{barberDrawerForm.name}}</el-descriptions-item>
                        <el-descriptions-item label="拼音">{{barberDrawerForm.spellName}}</el-descriptions-item>
                        <el-descriptions-item label="等级"><el-tag size="small">{{barberDrawerForm.grade}}</el-tag></el-descriptions-item>
                        <el-descriptions-item label="手机号码">{{barberDrawerForm.phone}}</el-descriptions-item>
                        <el-descriptions-item label="手艺值" :span="2" >{{barberDrawerForm.craftValue}}</el-descriptions-item>
                        <el-descriptions-item label="工作年限" :span="2" >{{barberDrawerForm.workYear}}</el-descriptions-item>
                    </el-descriptions>
                </el-main>
            </div>
        </el-drawer>
        <!-- ---------------------------------------------------------------------------------------------------------------------- -->
        <!-- 编辑弹出框 -->
        <el-dialog title="服务员编辑" v-model="editDialogVisible" width="55%" :close-on-click-modal="false">
            <VBarber v-bind:selectValue="VBarberData" @barberData="func_loadVBarberData" />
        </el-dialog>
        <!-- ---------------------------------------------------------------------------------------------------------------------- -->
        <!-- 图片弹出框 -->
        <el-dialog v-model="showPicVisible" append-to-body :width="bigPicWidth">
            <span>
                <el-card :body-style="{ padding: '7px' }"><el-image :src="bigPicUrl" class="image" @load="onBigPicLoad"/></el-card>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { onMounted, ref, reactive, toRefs, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { findBarberListPage, findBarberInfoById, deleteBarberById } from "../../../api/hairmanager/barber";
import { NO_DATA_SELECT_ERROR, SUCCESS_DELDATA_DIALOG_MESSAGE, } from "../../../api/CommonErrorMessage.js";
import VBarber from './components/barberForm.vue';

export default {
    name: "barber",
    components: {VBarber, },
    setup() {
        const router = useRouter();
		const route = useRoute();
        const optColumnWidth = 150;
        const multipleSelection = ref([])
        //主数据条件查询
        const query = reactive({
            keyword: "",
            pageIndex: 1,
            pageSize: 10,
        });
        // 主数据总页数
        const dataTotal = ref(0);
        // 主数据表格
        const barberTableData = ref([]);
        // 表格详细数据抽拉层
        const drawerViewVisible = ref(false);
        // 主详细数据表格
        const barberDrawerForm = reactive({
            id: 0,
            headPortrait: "",
            name: "",
            spellName: "",
            grade: "",
            phone: "",
            craftValue: 0,
            workYear: 0,
            memberId: 0,
        });
        // 表格编辑时弹窗和保存
        const editDialogVisible = ref(false);
        const VBarberData = reactive({
            load: false,
            id: 0,
        });
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findBarberListPage(query).then((res) => {
                barberTableData.value = res.data.list;
                dataTotal.value = res.data.totalPage;
            });
        };
        func_getData();
        /**
         * @name 主数据查询操作
         */
        const func_handleSearch = () => {
            query.pageIndex = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const func_handlePageChange = (val) => {
            query.pageIndex = val;
            func_getData();
        };
        /**
         * @name 主数据编辑对话框操作
         */
        const func_handleAdd = () => {
            editDialogVisible.value = true;
        }
        let idx = -1;
        /**
         * @name 读取服务员详细数据
         */
        const func_handleEdit = (index, row) => {
            idx = index;
            VBarberData['load'] = true;
            VBarberData['id'] = row['id'];
            editDialogVisible.value = true;
        };
        /**
         * @name 删除服务员数据操作
         */
        const func_handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", { type: "warning", }).then(() => {
                func_del(row['id']);
            }).catch(() => {});
        };
        const func_handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
        // 主业务处理 ========================================================================================= END
        /**
         * @name 删除服务员配置
         */
        const func_del = (id) => {
            deleteBarberById(id).then((res)=>{
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.data.msg);
                    } else {
                        ElMessage.error(res.data.msg);
                    }
                } else {
                    ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        // 扩展业务处理 ========================================================================================= START
        /**
         * @name 打开弹出层
         */
        const func_handleDrawerOpen = (index, row) => {
            findBarberInfoById(row['id']).then((res) => {
                Object.keys(barberDrawerForm).forEach((item) => {
                    barberDrawerForm[item] = res.data[item];
                });
            });
            drawerViewVisible.value=true;
        };
        // 扩展业务处理 ========================================================================================= START
        const func_loadVBarberData = (v) => {
            if(v['state'] === 1){
                editDialogVisible.value = false;
                VBarberData['load'] = null;
                func_getData();
            } else {
                editDialogVisible.value = true;
            }
        };
        // 扩展业务处理 ========================================================================================= END
        // 大图处理 ========================================================================================= START
        const showPicVisible = ref(false);
        const bigPicWidth = ref(0);
        const bigPicUrl = ref('');
        const func_showBigPic = (index, row) => {
            bigPicUrl.value = row.headPortrait;
            showPicVisible.value = true;
        };
        // 大图处理
        const onBigPicLoad = (e) => {
            const img = e.target;
            let width = 0;
            if (img.fileSize > 0 || (img.width > 1 && img.height > 1)) {
            width = img.width + 40;
            }
            bigPicWidth.value = width + 'px';
        };
        // 大图处理 ========================================================================================= END
        // =========================================================================================================
        return {
            // 系统环境变量 =========================================================================================
            optColumnWidth, query, dataTotal, multipleSelection, barberTableData,
            // 主业务调用方法 =========================================================================================
            func_handleSearch, func_handlePageChange, func_getData,  func_handleAdd, func_handleEdit, func_handleDelete, func_handleSelectionChange,
            // 数据执行处理 =========================================================================================
            func_getData, func_del, func_batchDel,
            // 扩展业务处理 =========================================================================================
            VBarberData, editDialogVisible, func_loadVBarberData,
            // 扩展业务处理 =========================================================================================
            barberDrawerForm, drawerViewVisible, func_handleDrawerOpen, 
            // 大图处理 =========================================================================================
            showPicVisible, onBigPicLoad, bigPicUrl, bigPicWidth, 
        };
    },

};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.el-row {
    margin-bottom: 20px;
}
.el-row:last-child {
    margin-bottom: 0;
}
.el-col {
    border-radius: 4px;
}
.grid-label {
    background: #00000;
    min-height: 36px;
    padding-left: 25px;
}
.grid-content {
    background: #00000;
    min-height: 36px;
    padding-left: 6px;
}
.row-bg {
    padding: 0px 0px 0px 0px;
}
.drawer-box-card {
    width:95%;
    margin-left:15px;
    overflow: auto;
}
.drawer-box-tabs {
    width:95%;
    margin-top:10px;
    margin-left:15px;
    overflow: auto;
    height: 900px;
}
.avatar {
	width: 150px;
	height: 150px;
	display: block;
	align: center;
}
.el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.el-dropdown-link {
  cursor: pointer;
  /* color: var(--el-color-primary); */
  color: #67c23a;
  display: flex;
  align-items: center;
}
</style>